home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Graphics / STIMP_noise / source / pnmwhitenoise / pnmwhitenoise.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  4.2 KB  |  203 lines

  1.  
  2. /************************************************************************/
  3. #define OP_NAME      "pnmwhitenoise"
  4. #define VERSION      "1.02"
  5. #define DATE         "31.01.98"
  6. #define AUTHOR       "Stefan Diener"
  7. /************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <math.h>
  14. #include <time.h>
  15. #include <sys/types.h>
  16.  
  17. #include <STIMP/pnm.c>
  18.  
  19. struct PNM_Info source;
  20. static BOOL Gleichv=TRUE;
  21. static int Streuung=75;
  22.  
  23. double RND_EQU(void)
  24. /* Gleichverteilung */
  25. {
  26.   long int a=31415821, x, y;
  27.   long int p=100000;   // p=100000000;
  28.   long int am, rm, q, ar, rr;
  29.  
  30.   y=(long int) rand();
  31.   q=(long int) sqrt((double) p); 
  32.  
  33.   am=(long int) a/q;
  34.   ar=a%q;
  35.  
  36.   rm=(long int) y/q;
  37.   rr=y%q;
  38.  
  39.   x=q*((am*rr+ar*rm)%q)+ar*rr;
  40.   y=(x+1)%p;
  41.  
  42.   return (double)y/(double)p;
  43. }
  44.  
  45. double RND_NORM(void)
  46. /* Normalverteilung */
  47. {
  48.   double t=-0.5, r, s;
  49.  
  50.   while ((t<0) || (t>1))
  51.   {
  52.     r=2*RND_EQU()-1;
  53.     s=2*RND_EQU()-1;
  54.     t=r*r+s*s;
  55.   }
  56.  
  57.   return (fabs(r)*sqrt((-2.0)*log(t)/t)/2.0);
  58. }
  59.  
  60. void Do_It(void)
  61. {
  62.   int i, w, ende;
  63.   unsigned char *srcR,*srcG, *srcB;
  64.  
  65.   if (source.type==TYPE_PPM)
  66.   {
  67.     srcR=source.redDATA;
  68.     srcG=source.greenDATA;
  69.     srcB=source.blueDATA;
  70.   }
  71.   else srcR=source.DATA;
  72.  
  73.   /* neue Folge von Zufallszahlen erzeugen */
  74.   srand((unsigned)time(NULL));
  75.  
  76.   /* Anzahl von Bildpunkten */
  77.   ende=source.height*source.width;
  78.  
  79.   if (Gleichv)   /* Gleichverteilung */
  80.   {
  81.     for (i=0; i<ende; i++)
  82.     {
  83.       w=(int) floor((*srcR)+Streuung*RND_EQU());
  84.       *srcR++=(w<source.maxval) ? w : source.maxval;
  85.  
  86.       if (source.type==TYPE_PPM)
  87.       {
  88.         w=(int) floor((*srcG)+Streuung*RND_EQU());
  89.         *srcG++=(w<source.maxval) ? w : source.maxval;
  90.  
  91.         w=(int) floor((*srcB)+Streuung*RND_EQU());
  92.         *srcB++=(w<source.maxval) ? w : source.maxval;
  93.       }
  94.     }
  95.   }
  96.   else   /* Normalverteilung */
  97.   {
  98.     for (i=0; i<ende; i++)
  99.     {
  100.       w=(int) floor((*srcR)+Streuung*RND_NORM());
  101.       *srcR++=(w<source.maxval) ? w : source.maxval;
  102.  
  103.       if (source.type==TYPE_PPM)
  104.       {
  105.         w=(int) floor((*srcG)+Streuung*RND_NORM());
  106.         *srcG++=(w<source.maxval) ? w : source.maxval;
  107.  
  108.         w=(int) floor((*srcB)+Streuung*RND_NORM());
  109.         *srcB++=(w<source.maxval) ? w : source.maxval;
  110.       }
  111.     }
  112.   }
  113. }
  114.  
  115. int main(int argc,char **argv)
  116. /* Hauptprogramm */
  117. {
  118.   int i;
  119.   unsigned char tempo[10];
  120.  
  121.   /* offizielle Begruessung */
  122.   PrintOpening(argc,argv);
  123.  
  124.   /* Uebergebene Parameter auswerten */
  125.   for (i=1; i<argc; i++)
  126.   {
  127.     if ((argv[i][0]=='-') && argv[i][1])
  128.     {
  129.       switch (argv[i][1])
  130.       {
  131.         case 'e': Gleichv=TRUE;
  132.                       break;
  133.  
  134.         case 'n': Gleichv=FALSE;
  135.                       break;
  136.  
  137.         case 's': strncpy(tempo, argv[i], 9);
  138.                       tempo[0]=32;
  139.                       tempo[1]=32;
  140.                       if (sscanf(tempo,"%i",&Streuung)!=1) Streuung=-1;
  141.                       if ((Streuung<0) || (Streuung>150))
  142.                       {
  143.                         PrintMessage("Wrong value for the scatter !");
  144.                         Hilfe();
  145.                         exit(-1);
  146.                       }
  147.                       break;
  148.  
  149.         case 'v': beVerbose=FALSE;
  150.                       break;
  151.  
  152.         default: PrintMessage("Unknown parameter: %s", argv[i]);
  153.                      Hilfe();
  154.                      exit(-1);
  155.                      break;
  156.       }
  157.     }
  158.  
  159.     if (argv[i][0]=='+')
  160.     {
  161.       switch (argv[i][1])
  162.       {
  163.         case 'v': beVerbose=TRUE;
  164.                       break;
  165.  
  166.         default: PrintMessage("Unknown parameter: %s", argv[i]);
  167.                      Hilfe();
  168.                      exit(-1);
  169.                      break;
  170.       }
  171.     }
  172.   }
  173.  
  174.   /* Mindestzahl der Argumente pruefen */
  175.   if (argc<3)
  176.   {
  177.     PrintMessage("Wrong number of arguments !");
  178.     Hilfe();
  179.     exit(-1);
  180.   }
  181.  
  182.   /* Anzahl der Dateinamen überprüfen */
  183.   if (FilenameCount(argc, argv)!=2)
  184.   {
  185.     PrintMessage("Wrong number of file names !");
  186.     Hilfe();
  187.     exit(-1);
  188.   }
  189.  
  190.   if (ReadPNMFile(GetFilename(1,argc,argv), TYPE_PNM, &source)==0)
  191.   {
  192.     PrintMessage("Working ...");
  193.     Do_It();
  194.  
  195.     WritePNMFile(GetFilename(2,argc,argv), &source);
  196.     FreePNMArray(&source);
  197.   }
  198.  
  199.   PrintClosing();
  200.   exit(0);
  201. }
  202.  
  203.